package com.ruoyi.common;


import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.LineType;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.List;

public class DrawElementsTool {
    //创建一个PdfDocument对象
    private PdfDocument doc = new PdfDocument();
    private DrawElementsFactory drawElementsFactory;

    private ServerConfig serverConfig = new ServerConfig();
    public DrawElementsTool(DrawElementsFactory drawElementsFactory) {
        this.drawElementsFactory = drawElementsFactory;
    }

    //绘制试卷元素
    public DrawElementsTool doDrawElements() {
        PdfSolidBrush brush;
        PdfPen pen;
        //添加页面
        PdfPageBase page;
        for (List<DrawElements> drawElementsList : this.drawElementsFactory.getDrawElementsList()) {
            page = doc.getPages().add();
            for (DrawElements drawElements : drawElementsList) {
                if (drawElements instanceof TextDrawElements) {
                    //在页面绘制题目
                    if (drawElements.getX() == -99) {//居中对齐
                        PdfTextAlignment alignment = PdfTextAlignment.Center;
                        PdfDrawObject.format.setAlignment(alignment);
                        if (((TextDrawElements) drawElements).getFontType() == FontType.大标题) {
                            brush = new PdfSolidBrush(new PdfRGBColor(Color.black));
                            page.getCanvas().drawString(((TextDrawElements) drawElements).getText(), PdfDrawObject.largeTitleFont, brush, page.getCanvas().getClientSize().getWidth() / 2,
                                    drawElements.getY(), PdfDrawObject.format);
                        } else if (((TextDrawElements) drawElements).getFontType() == FontType.小标题) {
                            brush = new PdfSolidBrush(new PdfRGBColor(Color.black));
                            page.getCanvas().drawString(((TextDrawElements) drawElements).getText(), PdfDrawObject.smallTitleFont, brush, page.getCanvas().getClientSize().getWidth() / 2,
                                    drawElements.getY(), PdfDrawObject.format);
                        } else if (((TextDrawElements) drawElements).getFontType() == FontType.拼音) {
                            brush = new PdfSolidBrush(new PdfRGBColor(Color.black));
                            page.getCanvas().drawString(((TextDrawElements) drawElements).getText(), PdfDrawObject.pinYFont, brush, page.getCanvas().getClientSize().getWidth() / 2,
                                    drawElements.getY(), PdfDrawObject.format);
                        } else if (((TextDrawElements) drawElements).getFontType() == FontType.方格字) {
                            brush = new PdfSolidBrush(new PdfRGBColor(drawElements.getColor()));
                            page.getCanvas().drawString(((TextDrawElements) drawElements).getText(), PdfDrawObject.gridFont, brush, page.getCanvas().getClientSize().getWidth() / 2,
                                    drawElements.getY(), PdfDrawObject.format);
                        }

                    } else {

                        if (((TextDrawElements) drawElements).getFontType() == FontType.大标题) {
                            brush = new PdfSolidBrush(new PdfRGBColor(Color.black));
                            page.getCanvas().drawString(((TextDrawElements) drawElements).getText(), PdfDrawObject.largeTitleFont, brush, drawElements.getX(), drawElements.getY(), PdfDrawObject.format);
                        } else if (((TextDrawElements) drawElements).getFontType() == FontType.小标题) {
                            brush = new PdfSolidBrush(new PdfRGBColor(Color.black));
                            page.getCanvas().drawString(((TextDrawElements) drawElements).getText(), PdfDrawObject.smallTitleFont, brush, drawElements.getX(), drawElements.getY(), PdfDrawObject.format);
                        } else if (((TextDrawElements) drawElements).getFontType() == FontType.拼音) {
                            brush = new PdfSolidBrush(new PdfRGBColor(Color.black));
                            page.getCanvas().drawString(((TextDrawElements) drawElements).getText(), PdfDrawObject.pinYFont, brush, drawElements.getX(), drawElements.getY(), PdfDrawObject.format);
                        } else if (((TextDrawElements) drawElements).getFontType() == FontType.方格字) {
                            brush = new PdfSolidBrush(new PdfRGBColor(drawElements.getColor()));
                            page.getCanvas().drawString(((TextDrawElements) drawElements).getText(), PdfDrawObject.gridFont, brush, drawElements.getX(), drawElements.getY(), PdfDrawObject.format);
                        }

                    }
                } else if (drawElements instanceof LineDrawElements) {

                    if(((LineDrawElements) drawElements).getLineType() == LineType.solid){
                        pen = new PdfPen(new PdfRGBColor(drawElements.getColor()), ((LineDrawElements) drawElements).getPenWidth());
                        page.getCanvas().drawLine(pen, drawElements.getX(), drawElements.getY(),
                                ((LineDrawElements) drawElements).getToX(), ((LineDrawElements) drawElements).getToY());
                    }

                    else
                    {
                        pen = new PdfPen(new PdfRGBColor(drawElements.getColor()), ((LineDrawElements) drawElements).getPenWidth());
                        //设置虚线样式
                        pen.setDashStyle(PdfDashStyle.Dash);
                        pen.setDashPattern(new float[]{2, 2, 2});
                        page.getCanvas().drawLine(pen, drawElements.getX(), drawElements.getY(),
                                ((LineDrawElements) drawElements).getToX(), ((LineDrawElements) drawElements).getToY());
                    }

                } else if (drawElements instanceof RectangleDrawElements) {
                    pen = new PdfPen(new PdfRGBColor(drawElements.getColor()), ((RectangleDrawElements) drawElements).getPenWidth());
                    Rectangle2D.Float rect1 = new Rectangle2D.Float(drawElements.getX(), drawElements.getY(),
                            ((RectangleDrawElements) drawElements).getWidth(), ((RectangleDrawElements) drawElements).getHeight());
                    page.getCanvas().drawRectangle(pen, null, rect1);

                }
            }
        }
        return this;
    }


    public void SavePdfFile(String fileName) {

        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        String url1 = serverConfig.getUrl();
        // 上传并返回新文件名称
        //保存文件
        doc.saveToFile(filePath + "\\" + fileName);

    }

}
